package com.googlecode.jch.sample;

import java.io.*;
import java.util.InputMismatchException;
import java.util.StringTokenizer;

/**
 * todo
 *
 * @author Iskander Akishev
 */
public class SimpleInputReader implements Closeable {
	private final BufferedReader reader;
	private StringTokenizer stringTokenizer = new StringTokenizer("");

	public SimpleInputReader(InputStream inputStream) {
		reader = new BufferedReader(new InputStreamReader(inputStream));
	}

	private void advance() {
		try {
			while (stringTokenizer != null && !stringTokenizer.hasMoreTokens()) {
				String line = reader.readLine();
				stringTokenizer = line == null ? null : new StringTokenizer(line);
			}
		} catch (IOException e) {
			throw new InputMismatchException(e.getMessage());
		}
	}

	public void close() throws IOException {
		reader.close();
	}

	public boolean hasMore() {
		advance();
		return stringTokenizer != null;
	}

	public String read() {
		if (stringTokenizer == null) {
			throw new InputMismatchException("End of stream was reached");
		}
		advance();
		return stringTokenizer.nextToken();
	}

	public int readInt() {
		return Integer.parseInt(read());
	}

	public long readLong() {
		return Long.parseLong(read());
	}

	public double readDouble() {
		return Double.parseDouble(read());
	}
}
